package com.baiytfp.hf.HFDataExchange.action.dao;

import java.sql.Connection;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import com.baiytfp.hf.HFDataExchange.common.model.base.TableName;
import com.baiytfp.hf.HFDataExchange.common.model.state.TreeType;
import com.baiytfp.hf.HFDataExchange.common.model.table.Data_dict;
import com.baiytfp.hf.baiytfp.framework.base.BaseDao;
import com.baiytfp.hf.baiytfp.hfutils.HFDBHelper;


@Repository
public class DataDictDAOImpl extends BaseDao {
	
	/**
	 * 判断dictVal是否已经存在
	 * @param dictVal
	 * @param parent_tree_code 
	 * @param connection 
	 * @return
	 * @throws Exception
	 */
	public boolean has_Dict_val(String dictVal, String parent_tree_code) throws Exception{
		long res=0;
		if(parent_tree_code.equals("-1")||StringUtils.isEmpty(parent_tree_code)) {
			res=HFDBHelper.rownum(null, " select * from "+TableName.DATA_DICT+" where  dict_val=? and tree_id in (select id from "+TableName.TREE+" where tree_type=? and parent_tree_code=-1) ", new String[]{dictVal,TreeType.DATA_DICT.getKey()+""});
		}else {
			res=HFDBHelper.rownum(null, " select * from "+TableName.DATA_DICT+" where  dict_val=? and tree_id in (select id from "+TableName.TREE+" where tree_type=? and parent_tree_code=?) ", new String[]{dictVal,TreeType.DATA_DICT.getKey()+"",parent_tree_code});
		}
		
		return res > 0 ? true : false;
	}
	
	/**
	 * 通过dataDictType获取数据字典列表
	 * @param dataDictType
	 * @param connection
	 * @return
	 * @throws Exception
	 */
	public List<Data_dict> getDataDict(String dataDictType) throws Exception {
		StringBuffer sql=new StringBuffer(" select dd.* from "+TableName.DATA_DICT+" dd ");
				sql.append(" left join "+TableName.TREE+" tree on dd.tree_id=tree.id ")
				.append(" where  tree.parent_tree_code=")
				.append("(select t.tree_code from "+TableName.DATA_DICT+" sdd ")
				.append(" left join "+TableName.TREE+" t on sdd.tree_id=t.id ")
				.append(" where t.parent_tree_code=-1 and sdd.dict_val=? ) ");
		return HFDBHelper.querySql(sql.toString(), Data_dict.class, new String[]{dataDictType});
		
	}

}
